<template>
	<view>
		<view class="searchBar">
			<uni-search-bar style="width: 100%;" radius="100" placeholder="请输入搜索内容" @confirm="search"
				v-model="searchValue" @clear="tianValue" cancelButton="none" @focus="inputValue">
				{{ searchValue }}
			</uni-search-bar>
			<view class="searchValue" v-if="showTrue" @click="searchText">
				<text>搜索</text>
			</view>
		</view>
		<uni-list :border="true">
			<block v-for="(item,index) in list" :key="index">
				<uni-list-item clickable @click="check(item)" :title="item.cropName">
				</uni-list-item>
			</block>
		</uni-list>

		<view class="" v-for="item in skyList">
			<uni-list :border="true">
				<uni-list-item clickable @click="checkTian(item)" :title="item.companyName"
					:note="item.base === null ? '暂无' : item.base"></uni-list-item>
			</uni-list>
		</view>
		<view class="searchCompany" @click="manSearch" v-if="checkLength>0">查询更多
			<uni-icons custom-prefix="custom-icon" type="search" size="20" />
		</view>
		<!-- 返回顶部按钮 -->
		<view class="topBack" @tap="toTop" :style="{ display: flag === false ? 'none' : 'block' }">
			<view class="backText">
				<text>^</text>
				<text>顶部</text>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		debounce,
		showLoading
	} from '@/config/debounce.js'
	export default {
		name: 'upTop',
		data() {
			return {
				showTrue: '',
				chakeNull: '',
				fication: '',
				androidIos: '',
				cropName: '',
				checkLength: '',
				appId: '',
				flag: false,
				// 页码
				pageNo: 1,
				// 每页数量
				pageSize: 20,
				// 搜索条件
				searchValue: '',
				// 是否前往天眼查查询
				isOpenOtherSearch: 0,
				// 公司code
				shipyardCode: '',
				// 公司名称
				companyName: '',
				totalPages: 1,
				// 公司地址
				companyAddress: '',
				ifBottomRefresh: false,
				loadmore: 'more',
				contentText: {
					contentdown: '加载',
					contentrefresh: '加载中...',
					contentnomore: '暂无更多数据。'
				},
				limitLoad: false, // 防止重复加载
				list: [],
				skyList: []
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		//第一次加载
		onShow(e) {
			// this.getList();
			this.fication = 1
		},
		onLoad() {
			this.androidIos = getApp().globalData.androidIos
		},
		onPageScroll(e) {
			//根据距离顶部距离是否显示回到顶部按钮
			if (e.scrollTop > 20) {
				//当距离大于10时显示回到顶部按钮
				this.flag = true;
			} else {
				this.flag = false;
			}
		},
		methods: {
			// 搜索条件绑定
			getList: debounce(function() {
				if (this.limitLoad) return
				this.limitLoad = true
				showLoading({
					mask: true
				})
				let data = {
					appId: this.androidIos,
					searchValue: this.searchValue,
					pageNo: this.pageNo,
					pageSize: this.pageSize,

				};
				// 更大返回内搜索调接口
				// 0是有,1是到天眼查
				this.$httpNew.post('authApi/EppSysCorp/GetEppSysCorpList', data).then(res => {
					this.checkLength = res.items.length
					this.limitLoad = false
					uni.hideLoading()
					if (res.items.length == 0) {
						this.tianChake() //天眼查数据查询
					}
					if (this.ifBottomRefresh) {
						this.list = this.list.concat(res.items);
					} else {
						this.list = res.items
					}
					this.ifBottomRefresh = false;
					this.loadmore = this.pageNo < res.totalPages ? 'more' : 'noMore';
				}).catch(err => {
					uni.hideLoading()
					this.limitLoad = false
				})
			}, 300),
			onPullDownRefresh() {
				this.pageNo = 1;
				this.ifBottomRefresh = false;
				this.getList();
				setTimeout(() => {
					//结束下拉刷新
					uni.stopPullDownRefresh();
				}, 1000);
			},
			onReachBottom() {
				this.pageNo += 1;
				if (this.loadmore == 'noMore') return;
				this.ifBottomRefresh = true;
				this.getList(); // 触底 拉取数据
			},
			// 更大返回内搜索
			search() {
				this.pageNo = 1;
				this.isOpenOtherSearch = 0;
				this.ifBottomRefresh = false;
				this.getList();
			},
			manSearch() {
				if (this.searchValue == '') {
					uni.showToast({
						title: '请输入要查询的公司',
						icon: 'none'
					});

				} else {
					this.list = []
					this.pageNo = 1;
					this.isOpenOtherSearch = 1;
					this.ifBottomRefresh = false;
					this.tianChake() //天眼查数据查询
				}

			},
			inputValue(e) {
				this.showTrue = true;
				console.log(e, 1111111111);

			},
			check(item) {
				// console.log(item.id, '认证企业');
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2]; //上一个页面
				//uni或者vue用$vm
				prevPage.$vm.fication = this.fication;
				prevPage.$vm.companyName = item.cropName;
				prevPage.$vm.companyId = item.id;
				let data = pages[pages.length - 1].options;
				uni.navigateBack({
					//返回
					delta: 1
				});

			},
			checkTian(item) {
				// console.log(item, '天眼查');
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2]; //上一个页面
				prevPage.$vm.companyName = item.companyName;
				prevPage.$vm.companyId = item.id;
				let data = pages[pages.length - 1].options;
				uni.navigateBack({
					//返回
					delta: 1
				});
			},
			// 点击返回顶部
			toTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 100
				});
			},

			// 天眼查数据查询
			tianChake() {
				let data = {
					appId: this.androidIos,
					searchValue: this.searchValue,
					pageNo: this.pageNo,
					pageSize: this.pageSize,
				}
				this.$httpNew.post('authApi/TianyanchaCompany/GetListForTianYanCha', data).then(skyChake => {
					console.log(skyChake, 'skyChake161');
					this.chakeNull = skyChake.items
					if (skyChake.items == null) {
						uni.showToast({
							title: '查询的数据暂无',
							icon: 'none'
						});
						return;
					}

					if (this.ifBottomRefresh) {
						this.skyList = this.skyList.concat(skyChake.items)
					} else {
						this.skyList = skyChake.items
					}
					this.ifBottomRefresh = false;
					// this.getList();
					this.loadmore = this.pageNo < skyChake.totalPages ? 'more' : 'noMore';
				}).catch(errSky => {
					uni.hideLoading()
					this.limitLoad = false
				})
			},
			tianValue(res) {
				this.chakeNull = false;
				this.showTrue = false;
				let data = {
					appId: this.androidIos,
					searchValue: this.searchValue,
					pageNo: this.pageNo,
					pageSize: this.pageSize,

				};

				this.$httpNew.post('authApi/EppSysCorp/GetEppSysCorpList', data).then(res => {})
			},
			searchText() {
				this.manSearch()
			}
		}
	};
</script>

<style lang="less" scoped>
	page {
		background-color: #fff;
	}

	.searchBar {
		width: 100%;
		background-color: #fff;
		// position: fixed;
		// top: 0;
		// z-index: 99;
		display: flex;
		justify-content: space-between;

		/deep/.uni-searchbar__box {
			border: 1px solid #F8F9F8;
		}
	}

	.searchValue {
		width: 100rpx;
		text-align: center;
		line-height: 145rpx;
		font-size: 30rpx;
	}

	.searchCompany {
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	// 返回顶部按钮
	.topBack {
		position: fixed;
		width: 80rpx;
		height: 80rpx;
		align-items: center;
		display: none;
		left: 660rpx;
		top: 630px;

		.backText {
			display: flex;
			flex-direction: column;
			align-items: center;
		}
	}
</style>
